package com.wanchuang.shengfubao.module.account;

import android.content.Intent;
import android.os.Bundle;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;


import com.wanchuang.shengfubao.R;
import com.wanchuang.shengfubao.base.BaseActivity;
import com.wanchuang.shengfubao.net.Apis;
import com.wanchuang.shengfubao.net.NetworkManager;
import com.wanchuang.shengfubao.net.interfaces.OnRequestCallBack;
import com.wanchuang.shengfubao.proto.Connect;
import com.wanchuang.shengfubao.view.VerificationCodeEditText;

import butterknife.BindView;
import butterknife.ButterKnife;

public class TiXianPayActivity extends BaseActivity {

    @BindView(R.id.back_iv)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.input_code)
    VerificationCodeEditText inputCode;

    @BindView(R.id.tv_message)
    TextView tvMessage;
    @BindView(R.id.tv_money)
    TextView tvMoney;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ti_xian_pay);
        ButterKnife.bind(this);
        tvTitle.setText("提现支付密码");
        tvMessage.setText("提现至 " + getIntent().getStringExtra("message"));
        tvMoney.setText("￥" + getIntent().getStringExtra("money"));
        ivBack.setOnClickListener(v -> finish());
        inputCode.setOnInputListener(new VerificationCodeEditText.OnInputListener() {
            @Override
            public void OnEdittextChange(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void OnInputOk(String codeNum) {
                Connect.sms_register.Builder builder = Connect.sms_register.newBuilder();
                builder.setPayword(codeNum);
                showProgress();
                NetworkManager.INSTANCE.post(Apis.check_pd_pwd2, builder.build().toByteArray(), new OnRequestCallBack() {
                    @Override
                    public void onOk(byte[] response) {

                    }

                    @Override
                    public void onError(int errorCode, String errorMessage) {
                        hideProgress();
                        if (errorCode == 200) {
                            Intent intent = new Intent(mContext, TiXianCodeActivity.class);
                            intent.putExtra("money",  getIntent().getStringExtra("money"));
                            intent.putExtra("password", codeNum);
                            intent.putExtra("card_id",getIntent().getStringExtra("card_id"));
                            intent.setType(getIntent().getType());
                            startActivity(intent);
                            finish();
                        } else {
                            showToast(errorMessage);
                            inputCode.clean();
                        }
                    }
                });


            }
        });
        showSoftInputFromWindow(inputCode.editCodeNum);
    }

    public void showSoftInputFromWindow(EditText editText) {
        editText.setFocusable(true);
        editText.setFocusableInTouchMode(true);
        editText.requestFocus();
        this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    }
}
